package com.cloudfast.oauth.translator;

import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.common.exceptions.InvalidClientException;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.common.exceptions.InvalidScopeException;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.common.exceptions.UnsupportedGrantTypeException;
import org.springframework.security.oauth2.provider.NoSuchClientException;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
import org.springframework.web.HttpRequestMethodNotSupportedException;

import com.cloudfast.common.result.ApiResponse;
import com.cloudfast.context.error.CloudException;
import com.cloudfast.context.error.ErrorCode;

/**
 * 登录失败异常返回
 *
 * @author liuyw
 * @date 2022年8月29日
 */
public class ResponseExceptionTranslator implements WebResponseExceptionTranslator<OAuth2Exception> {
    private static final String TOKEN = "token";
    private static final String AUTHORIZATION = "authorization";

    @SuppressWarnings({"unchecked", "rawtypes"})
    @Override
    public ResponseEntity translate(Exception e) {
        ApiResponse apiResult = null;
        if (e instanceof InvalidGrantException) {
            if (e.getMessage().indexOf(TOKEN) > 0) {
                apiResult = ApiResponse.message(ErrorCode.TOKEN_VALID_ERROR);
            } else if (e.getMessage().indexOf(AUTHORIZATION) > 0) {
                apiResult = ApiResponse.message(ErrorCode.AUTHORIZATION_ERROR);
            } else {
                apiResult = ApiResponse.message(ErrorCode.USERNAME_PASSWORD_ERROR);
            }
        } else if (e instanceof InvalidClientException) {
            apiResult = ApiResponse.message(ErrorCode.GRANT_TYPE_ERROR);
        } else if (e instanceof InvalidTokenException || e instanceof InvalidScopeException) {
            apiResult = ApiResponse.message(ErrorCode.TOKEN_VALID_ERROR);
        } else if (e instanceof UnsupportedGrantTypeException) {
            apiResult = ApiResponse.message(ErrorCode.GRANT_TYPE_ERROR);
        } else if (e instanceof UsernameNotFoundException) {
            apiResult = ApiResponse.message(ErrorCode.USERNAME_PASSWORD_ERROR);
        } else if (e instanceof HttpRequestMethodNotSupportedException) {
            apiResult = ApiResponse.message(ErrorCode.METHOD_NOT_SUPPORTED);
        } else if (e instanceof CloudException) {
            CloudException es = (CloudException) e;
            if (es.getException() instanceof NoSuchClientException) {
                apiResult = ApiResponse.message(ErrorCode.CLIENT_ERROR);
            } else if (es.getException() instanceof UnsupportedGrantTypeException) {
                apiResult = ApiResponse.message(ErrorCode.GRANT_TYPE_ERROR);
            } else if (es.getErrorCode() != null) {
                apiResult = ApiResponse.message(es.getErrorCode());
            } else if (es.getException() instanceof CloudException) {
                CloudException cloudException = (CloudException) es.getException();
                if (cloudException.getErrorCode() != null) {
                    apiResult = ApiResponse.message(cloudException.getErrorCode());
                } else {
                    apiResult = ApiResponse.message(ErrorCode.SYSTEM_ERROR);
                }
            } else {
                apiResult = ApiResponse.message(ErrorCode.SYSTEM_ERROR);
            }
        } else {
            apiResult = ApiResponse.message(ErrorCode.SYSTEM_ERROR);
        }
        return new ResponseEntity<>(apiResult.getBody(), apiResult.getHttpStatus());
    }
}
